package com.mebs.model.service.accommodation;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com
 * Date: Mar 14, 2006, 2:51:23 PM
 */
@Entity()
@Table(name = "ACCOMOD_REPAIR")
public class AccommodationRepair {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "AccommodationId")
    private Accommodation accommodation;

    @Column(name = "ReportedDate", nullable = true)
    private Date reportedDate;

    @Column(name = "Reporter", nullable = true, length = 250)
    private String reporter;

    @Column(name = "FixedDate", nullable = true)
    private Date fixedDate;

    @Column(name = "Repairer", nullable = true, length = 250)
    private String repairer;

    @Column(name = "Description", nullable = false, length = 1024)
    private String description;

    @ManyToOne(optional = true)
    @JoinColumn(name = "FollowUpId")
    @Field(type = ArgumentType.INSTANCE)
    private AccommodationRepair followUpRepair;

    @Column(name = "FollowUpDate", nullable = true)
    private Date followUpDate;

    /**
     * **********************
     * Getters-setters
     * ***********************
     */

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Accommodation getAccommodation()
    {
        return accommodation;
    }

    public void setAccommodation(Accommodation accommodation)
    {
        this.accommodation = accommodation;
    }

    public Date getReportedDate()
    {
        return reportedDate;
    }

    public void setReportedDate(Date reportedDate)
    {
        this.reportedDate = reportedDate;
    }

    public String getReporter()
    {
        return reporter;
    }

    public void setReporter(String reporter)
    {
        this.reporter = reporter;
    }

    public Date getFixedDate()
    {
        return fixedDate;
    }

    public void setFixedDate(Date fixedDate)
    {
        this.fixedDate = fixedDate;
    }

    public String getRepairer()
    {
        return repairer;
    }

    public void setRepairer(String repairer)
    {
        this.repairer = repairer;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public AccommodationRepair getFollowUpRepair()
    {
        return followUpRepair;
    }

    public void setFollowUpRepair(AccommodationRepair followUpRepair)
    {
        this.followUpRepair = followUpRepair;
    }

    public Date getFollowUpDate()
    {
        return followUpDate;
    }

    public void setFollowUpDate(Date followUpDate)
    {
        this.followUpDate = followUpDate;
    }

    @Override
    public String toString()
    {
        return this.description;
    }

    @Transient
    public String getShortDesc() {
        if(this.description.length() > 25) {
            return this.description.substring(0,25) + "...";
        } else {
            return this.description;
        }
    }

    @Transient
    public boolean isDeletable()
    {
        Set<AccommodationRepair> repairs = this.accommodation.getRepairs();

        if (repairs != null) {
            for (AccommodationRepair repair : repairs) {
                if (this.equals(repair.followUpRepair)) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final AccommodationRepair that = (AccommodationRepair) o;
        return id.equals(that.getId());
    }

}
